package com.dehui.traders.ui.optioanl;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.databinding.ActivityAddOptionalStockBinding;

import me.goldze.mvvmhabit.base.BaseActivity;

/**
 * Created by hqq on 18.4.18.
 */

public class AddOptionalStockActivity extends BaseActivity<ActivityAddOptionalStockBinding,AddOptionalStockViewModel> implements AddOptionalStockViewModel.AddOptionalInterface {

    private AddOptionalStockViewModel viewModel;

    @Override
    public int initContentView() {
        return R.layout.activity_add_optional_stock;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public AddOptionalStockViewModel initViewModel() {
        viewModel = new AddOptionalStockViewModel(this, this,this);
        return viewModel;
    }

    public static void start(Activity context){
        Intent intent = new Intent(context, AddOptionalStockActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.refreshAdapter();
    }

    @Override
    public void initData() {
        super.initData();
        EditText etSearch = findViewById(R.id.et_search);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                if (TextUtils.isEmpty(str)){

                }else{
                    viewModel.searchStock(str);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                viewModel.onClickItem(position);
            }
        });
    }

    @Override
    public void finishView(){
        finish();
    }


}
